package com.mytijian.code.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mytijian.code.dao.CodeOperateLogDAO;
import com.mytijian.code.dao.dataobject.CodeOperateLogDO;
import com.mytijian.code.enums.CodeOperateEnum;
import com.mytijian.code.model.CodeOperateLog;
import com.mytijian.code.model.Mcode;
import com.mytijian.utils.BeanUtil;

/**
 * Created by shjh on 2017/9/26.
 */
@Service
public class CodeOperateLogServiceImpl implements CodeOperateLogService {
	
	private Logger logger = LoggerFactory.getLogger(CodeOperateLogServiceImpl.class);

	@Autowired
	private CodeOperateLogDAO codeOperateLogDAO;

	@Override
	public void saveCodeOperateLogs(List<Mcode> mcodes, CodeOperateEnum operateEnum) {
		if (mcodes == null || mcodes.isEmpty()) {
			logger.warn("保存code操作log，传入电子码列表为空");
			return;
		}
		List<CodeOperateLogDO> operateLogDOs = new ArrayList<>();
		mcodes.stream().forEach(mcode -> {
			CodeOperateLogDO codeOperateLogDO = createCodeOperateLog(mcode, operateEnum);
			operateLogDOs.add(codeOperateLogDO);
		});
		codeOperateLogDAO.batchInsert(operateLogDOs);
	}

	private CodeOperateLogDO createCodeOperateLog(Mcode mcode, CodeOperateEnum operateEnum) {
		CodeOperateLogDO operateLogDO = new CodeOperateLogDO();
		operateLogDO.setCode(mcode.getCode());
		operateLogDO.setOuterId(mcode.getOuterId());
		operateLogDO.setOperate(operateEnum.getCode());
		operateLogDO.setValidStart(mcode.getValidStart());
		operateLogDO.setValidEnd(mcode.getValidEnd());
		return operateLogDO;
	}

	@Override
	public int batchSave(List<CodeOperateLog> codeOperateLogs) {
		if (codeOperateLogs == null || codeOperateLogs.isEmpty()) {
			logger.warn("保存code操作log，传入电子码列表为空");
			return 0;
		}
		return codeOperateLogDAO.batchInsert(BeanUtil.copyBeanList(codeOperateLogs, CodeOperateLogDO.class));
	}
}
